package com.yourcompany.web.controller;

import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yourcompany.api.dto.RoleDTO;
import com.yourcompany.api.dto.RoleQueryDTO;
import com.yourcompany.api.dto.RolePermissionDTO;
import com.yourcompany.api.entity.Role;
import com.yourcompany.api.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Tag(name = "角色管理")
@RestController
@RequestMapping("/api/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Operation(summary = "创建角色")
    @PostMapping
    public SaResult createRole(@Validated @RequestBody RoleDTO roleDTO) {
        roleService.createRole(roleDTO);
        return SaResult.ok();
    }

    @Operation(summary = "更新角色")
    @PutMapping("/{id}")
    public SaResult updateRole(@PathVariable Long id, @Validated @RequestBody RoleDTO roleDTO) {
        roleService.updateRole(id, roleDTO);
        return SaResult.ok();
    }

    @Operation(summary = "删除角色")
    @DeleteMapping("/{id}")
    public SaResult deleteRole(@PathVariable Long id) {
        roleService.deleteRole(id);
        return SaResult.ok();
    }

    @Operation(summary = "获取角色详情")
    @GetMapping("/{id}")
    public SaResult getRoleById(@PathVariable Long id) {
        return SaResult.ok().setData(roleService.getRoleById(id));
    }

    @Operation(summary = "分页查询角色列表")
    @GetMapping("/list")
    public SaResult pageRoles(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "查询条件") RoleQueryDTO queryDTO) {
        Page<Role> page = new Page<>(pageNum, pageSize);
        return SaResult.ok().setData(roleService.pageRoles(page, queryDTO));
    }

    @Operation(summary = "获取所有可用角色")
    @GetMapping("/all")
    public SaResult listAllEnabledRoles() {
        return SaResult.ok().setData(roleService.listAllEnabledRoles());
    }

    @Operation(summary = "修改角色状态")
    @PutMapping("/{id}/status")
    public SaResult updateStatus(@PathVariable Long id, @RequestParam Boolean status) {
        roleService.updateStatus(id, status);
        return SaResult.ok();
    }

    @Operation(summary = "分配角色权限")
    @PostMapping("/permissions")
    public SaResult assignPermissions(@Validated @RequestBody RolePermissionDTO rolePermissionDTO) {
        roleService.assignPermissions(rolePermissionDTO);
        return SaResult.ok();
    }

    @Operation(summary = "获取角色权限")
    @GetMapping("/{id}/permissions")
    public SaResult getRolePermissions(@PathVariable Long id) {
        return SaResult.ok().setData(roleService.getRolePermissionIds(id));
    }
} 